package com.example.server;

import java.sql.Date;
import java.text.DateFormat;
import java.util.ArrayList;

import com.example.silvercare.R;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import app.database.friendlist.Constants;
import app.database.friendlist.FriendListDB;

public class FriendListActivity extends ListActivity {
	final int TABLE = 0;
	FriendListDB dba;
	FriendListAdapter adapter;
	
	private class Friend{
		public String name;
		public String email;
		public String recorddate;
		public Friend(String n, String e, String r){
			name = n;
			email = e;
			recorddate = r;
		}
		
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		// TODO Auto-generated method stub
		dba = new FriendListDB(this);
		dba.open();
		setContentView(R.xml.friendlist);

		super.onCreate(savedInstanceState);
		adapter=new FriendListAdapter(this);
		this.setListAdapter(adapter);
		
		registerForContextMenu(getListView());
	}
	
	private class FriendListAdapter extends BaseAdapter {
		private LayoutInflater mInflater;
		public ArrayList<Friend> friendlist;
		public FriendListAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
			friendlist = new ArrayList<Friend>();
			getdata();
		}
		public void getdata(){
			Cursor c = dba.getFriends(TABLE);
			startManagingCursor(c);
			if(c.moveToFirst()){
				do{
					String name = c.getString(c.getColumnIndex(Constants.FRIEND_NAME));
					String email = c.getString(c.getColumnIndex(Constants.EMAIL_NAME));
					DateFormat dateFormat = DateFormat.getDateTimeInstance();
					String datedata = dateFormat.format(new Date(c.getLong(c.getColumnIndex(Constants.DATE_NAME))).getTime());
					Friend f = new Friend(name, email, datedata);
					friendlist.add(f);
				}while(c.moveToNext());
			}
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return friendlist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return friendlist.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			final ViewHolder holder;
			View v  = convertView;
			if((v==null) || (v.getTag()==null)){
				v = mInflater.inflate(R.xml.friendlistrow, null);
				holder = new ViewHolder();
				holder.mName = (TextView)v.findViewById(R.id.name);
				holder.mEmail = (TextView)v.findViewById(R.id.email);
				v.setTag(holder);
			}else{
				holder = (ViewHolder)v.getTag();
			}
			holder.mfriend = (Friend) getItem(position);
			holder.mName.setText(holder.mfriend.name);
			holder.mEmail.setText(holder.mfriend.email);
			v.setTag(holder);
			return v;
		}
		
		public class ViewHolder {
			Friend mfriend;
			TextView mName;
			TextView mEmail;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.friendlist_option_menu, menu);
	    
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);
		
		switch (item.getItemId()) {
		case (R.id.addfriend):
			Intent i = new Intent("app.database.friendlist.Friend");
			i.putExtra("TABLE", TABLE);
			startActivity(i);
			return true;
		case (R.id.deleteall_friends):
			AlertDialog dialog = new AlertDialog.Builder(this).create();
			dialog.setMessage("Are you sure to delete all friends?");
			dialog.setButton(DialogInterface.BUTTON_POSITIVE, "YES", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					dba.deleteAllFriends(TABLE);
					onRestart();
				}
			});
			dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "NO", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					
				}
			});
			dialog.show();
			return true;
		default:
			return true;
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Intent friendlocation = new Intent("com.example.server.GetSingleFriendLocation");
		friendlocation.putExtra("FriendName", ((Friend)adapter.getItem(position)).name);
		friendlocation.putExtra("FriendEmail", ((Friend)adapter.getItem(position)).email);
		startActivity(friendlocation);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		dba.close();
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		adapter=new FriendListAdapter(this);
		this.setListAdapter(adapter);
		super.onRestart();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		//menu.add(0, R.id.menu_editfriend, 0, "Edit Friend");
		menu.add(0, R.id.menu_deletefriend, 0, "Delete Friend");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onContextItemSelected(item);
		
		switch(item.getItemId()){
		case R.id.menu_editfriend:
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		    int index = info.position;
		    Friend f = adapter.friendlist.get(index);
			Intent i = new Intent("app.database.friendlist.Friend");
			i.putExtra("NAME", f.name);
			i.putExtra("EMAIL", f.email);
			i.putExtra("TABLE", TABLE);
			startActivity(i);
			break;
		case R.id.menu_deletefriend:
			AdapterContextMenuInfo info1 = (AdapterContextMenuInfo) item.getMenuInfo();
		    int index1 = info1.position;
		    Friend f1 = adapter.friendlist.get(index1);
		    dba.deleteFriend(f1.email, TABLE);
		    onRestart();
		}
		return true;
	}

}